package com.weihua.flow;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class FlowCountReducer extends Reducer<Text, FlowBean, Text, FlowBean> {
    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {
        //1、定义接收上行流量和与下行流里和的变量;
        long sumUpFlow = 0;
        long sumDownFlow = 0;

        //2、遍历迭代器，提取每个上行与下行流量进行累加
        for (FlowBean value : values) {
            sumUpFlow += value.getUpFlow();
            sumDownFlow += value.getDownFlow();
        }

        //3、实例化FlowBean，封装最终结果数据
        FlowBean reduceValue = new FlowBean(sumUpFlow, sumDownFlow);

        //4、存储结果数据：
        context.write(key, reduceValue);


    }
}
